package com.vladimirkolarevic.eventplanner.tapestry.pages;

import java.text.DateFormat;
import java.text.Format;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextArea;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.vladimirkolarevic.eventplanner.Comment;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.Performer;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.CommentBean;
import com.vladimirkolarevic.eventplanner.service.EventsService;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;

public class EventInfo {

	private Long id;

	@ApplicationState
	private User user;
	private boolean userExists;

	@Inject
	private MembershipsService membership;

	private Form form;

	@Property
	@Persist("flash")
	private Event event;

	private List<Comment> comments;

	@Property
	@Persist
	private Comment comment;

	private Performer aPerformer;

	private Comment aComment;

	@Inject
	private EventsService events;

	private boolean commented;

	@BeginRender
	public void createEvent() {
		event = events.retriveEvent(id);
		comments = event.getComments();
		comment = new CommentBean();
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void onActivate(Long id) {
		this.id = id;
	}

	public Long onPassivate() {
		return id;
	}

	public Format getDateFormat() {
		return DateFormat.getDateInstance();
	}

	public List<Performer> getAllPerformers() {
		return event.getPerformers();
	}

	public Performer getaPerformer() {
		return aPerformer;
	}

	public void setaPerformer(Performer aPerformer) {
		this.aPerformer = aPerformer;
	}

	Object onSuccess() {

		// events.atach(event);
		comment.setUser(user);
		event.addComment(comment);
		events.update(event);
		return this;
	}

	public List<Comment> getAllComments() {
		return comments;
	}

	public Comment getaComment() {
		if (userExists) {
			if (aComment.getUser().getFirstName().equals(user.getFirstName())) {
				commented = true;
			}
		}
		return aComment;
	}

	public void setaComment(Comment aComment) {
		this.aComment = aComment;
		commented = false;
	}

	/*
	 * public Object onActivate(){ if (userExists) { return null; }return
	 * Index.class; }
	 */
	public boolean isUserExists() {
		return userExists;
	}

	@OnEvent(component = "removeComment")
	public Object removeComment(Long id) {

		Iterator<Comment> cit = event.getComments().iterator();
		while (cit.hasNext()) {
			Comment comment = (Comment) cit.next();
			if (comment.getId().equals(id)) {
				cit.remove();
				comment.setUser(null);
				// events.delete(comment);
			}
			events.update(event);

		}

		System.out.println("obriso " + id);

		return this;
	}

	public boolean isCommented() {
		return commented;
	}
}
